﻿using CarRental.DDL;
using CarRental.Helpers;
using CarRental.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace CarRental.View.Account {
    public partial class Register : System.Web.UI.Page {
        DbAccess __dbAccess = new DbAccess();
        protected void Page_Load(object sender, EventArgs e) {
            ddlUserType.DataSource = new CarRental.Helpers.OptionItems.UserTypeLst(false, Shared.LoginInfo.UserType);
            ddlUserType.DataValueField = "Key";
            ddlUserType.DataTextField = "Title";

            ddlUserType.DataBind();
        }

        protected void btnRegister_Click(object sender, EventArgs e) {
            string _email = tbEmail.Text;
            string _LoginName = tbLoginName.Text;
            var _lst = __dbAccess.GetUser().Where(w => (w.email.ToUpper() == _email.Trim().ToUpper() ||
                                                        w.login_name.ToUpper() == _LoginName.Trim().ToUpper()) &&
                                                        w.status == "A").ToList<user>();

            if (_lst.Any()) {
                lblWarning.Text = "Email address or Login name already exist. Please use the other email or name.";
            } else {
                user _user = new user();
                _user.login_name = tbLoginName.Text;
                _user.password = tbPassword.Text;
                _user.user_type = ddlUserType.SelectedValue;
                _user.user_name = tbUserName.Text;
                _user.address = tbAddress.Text;
                _user.contact_no = tbContactNo.Text;
                _user.status = "A";
                _user.email = tbEmail.Text;
                _user.remarks = tbRemark.Text;

                __dbAccess.InsertUser(_user);
                MessageBox.Show("Success", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

                Response.Redirect("~/View/Account/Login.aspx");
                //SendEmail _sendEmail = new SendEmail();
                //_sendEmail.Sendmail(tbEmail.Text, "Test", "HIHI");
            }
        }
    }
}